$(function () {
    page(null,1,10)
    $("#searchSubmit").click(function () {
        var names =  $("#recordSearch").val()
        page(names,1,10)
    })
})
function page(names,current,pageSize) {
    $.post("record/findRecord",{names:names,currentPage:current,pageSize:pageSize,},function (json) {
        if (json != "index.jsp") {
            if (json.flag) {
                var records = json.data
                $("#totalPage").html(records.totalPage)
                $("#totalCount").html(records.totalCount)
                var record_lis = ''
                for (var i = 0; i < records.list.length; i++) {
                    record = records.list[i]
                    var li = '<tr>\n' +
                        '            <td>' + (i + 1) + '</td>\n' +
                        '            <td>' + record.rid + '</td>\n' +
                        '            <td>' + record.name + '</td>\n' +
                        '            <td>' + record.time + '</td>\n' +
                        '            <td>' + record.door + '</td>\n' +
                        '        </tr>'
                    record_lis += li
                }
                $("#RTable").html(record_lis)
            }

            var beforeNum = records.currentPage-1;
            if (beforeNum <= 0){
                beforeNum = 1;
            }
            var lis = '<li onclick="javascript:page(\''+names+'\','+1+','+pageSize+')"><a href="javascript:void(0)">首页</a></li>\n' +
                '<li onclick="javascript:page(\''+names+'\','+beforeNum+','+pageSize+')"> <a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">&laquo;</span> </a></li>'
            //开始页码
            let begin;
            //结束页码
            let end;
            if (records.totalPage <= 10){
                begin = 1;
                end = records.totalPage;
            }else {
                begin = (records.currentPage - 5);
                end = (records.currentPage + 4);
                if (begin < 1){
                    begin = 1;
                    end = begin + 9;
                }
                if (end > records.totalPage){
                    end = records.totalPage;
                    begin = end - 9;
                }
            }
            for (let i = begin; i <= end; i++) {
                var l = ""
                if (records.currentPage == i) {
                    l = '<li class="active" onclick="javascript:page(\''+names+'\','+i +',' + pageSize + ')"><a href="javascript:void(0);">' + i + '</a></li>'
                } else {
                    l = '<li onclick="javascript:page(\''+names+'\','+i+','+pageSize +')"><a href="javascript:void(0);">' + i + '</a></li>'
                }
                lis += l
            }

            var nextNum = records.currentPage +1;
            if (nextNum >= records.totalPage){
                nextNum = records.totalPage;
            }
            lis += '<li onclick="javascript:page(\''+names+'\',' + nextNum + ',' + pageSize + ')"><a href="javascript:void(0);" aria-label="Next"><span aria-hidden="true">&raquo;</span></a> </li>\n' +
                '<li onclick="javascript:page(\''+names+'\',' + records.totalPage + ',' + pageSize + ')"><a href="javascript:void(0);">尾页</a></li>'

            $("#paging").html(lis)
        }else {
            location.href = json
        }
    })
    window.scrollTo(0,50);
}